﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using SuperGo.Silverlight.WebApp;

namespace SuperGo.Silverlight {
	public partial class ContinueExistingGame {
		public enum GameStates {
			InProgress,
			Requested,
			AlmostDone
		}

		public class ViewGame {
			public ViewGame(WsGame game) {
				GameState = GetGameState(game);
				GameId = game.GameId;
				GameIdAsString = game.GameId.ToString();
				Opponent = game.Opponent.Name;
				Status = GetStatus(GameState);
				Game = game;
			}

			private static GameStates GetGameState(WsGame game) {
				if (game.DateRequestAccepted == null)
					return GameStates.Requested;
				if (game.DateOpponentAcceptedDeadStones != null)
					return GameStates.AlmostDone;
				return GameStates.InProgress;
			}

			private string GetStatus(GameStates gameState) {
				switch (GameState) {
					case GameStates.InProgress:
						return "In Progress";
					case GameStates.Requested:
						return "Requested";
					case GameStates.AlmostDone:
						return "Almost Done";
				}
				throw new Exception("Invalid Game State " + gameState);
			}

			public WsGame Game { get; set; }
			public string Status { get; set; }
			public int GameId { get; set; }
			public string GameIdAsString { get; set; }
			public string Opponent { get; set; }
			public GameStates GameState { get; set; }
			public Visibility InProgress {
				get { return GameState == GameStates.InProgress || GameState == GameStates.AlmostDone ? Visibility.Visible : Visibility.Collapsed; } 
			}
			public Visibility Requested {
				get { return GameState == GameStates.Requested ? Visibility.Visible : Visibility.Collapsed; }
			}
		}

		private ObservableCollection<ViewGame> _activeGames { get; set; }

		public ContinueExistingGame() {
			try {
				InitializeComponent();
				Loaded += new RoutedEventHandler(ContinueExistingGame_Loaded);
			} catch (Exception ex) {
				HandleException(ex);
			}
		}

		public void SetActiveGames(IEnumerable<WsGame> activeGames) {
			try {
				_activeGames = new ObservableCollection<ViewGame>();
				List<ViewGame> activeWsGames = activeGames.Select(g => new ViewGame(g)).ToList();
				activeWsGames.ForEach(vg => _activeGames.Add(vg));
				_gamesInProgess.ItemsSource = _activeGames;
			} catch (Exception ex) {
				HandleException(ex);
			}
		}

		void ContinueExistingGame_Loaded(object sender, RoutedEventArgs e) {
			try {
				_gamesInProgess = (DataGrid)_continueAGamePanel.FindName("_gamesInProgess");
				_buttonPanel = (ButtonPanel)_continueAGamePanel.FindName("_buttonPanel");

				_startNewGame = (Button)_buttonPanel.FindName("_startNewGame");
				_cancel = (Button)_buttonPanel.FindName("_cancel");

				_startNewGame.Click += new RoutedEventHandler(StartNewGame_Click);
				_cancel.Click += new RoutedEventHandler(Cancel_Click);
			} catch (Exception ex) {
				HandleException(ex);
			}
		}

		void Cancel_Click(object sender, RoutedEventArgs e) {
			Page.ShowGamePage();
		}

		void ContinueExisting_Click(object sender, RoutedEventArgs e) {
			try {
				Button button = (Button)sender;
				int gameId = Convert.ToInt32(button.Tag);
				ViewGame selectedGame = _activeGames.Single(g => g.GameId == gameId);

				if (selectedGame != null) {
					Page.LoadGame(selectedGame.Game);
				}
			} catch (Exception ex) {
				HandleException(ex);
			}
		}

		void StartNewGame_Click(object sender, RoutedEventArgs e) {
			Page.ShowInitiateGame();
		}

		private void ViewGameRequest_Click(object sender, RoutedEventArgs e) {
			try {
				Button button = (Button)sender;
				int gameId = Convert.ToInt32(button.Tag);
				ViewGame selectedGame = _activeGames.Single(g => g.GameId == gameId);

				if (selectedGame != null) {
					Page.ShowInitiateGame(selectedGame.Game);
				}
			} catch (Exception ex) {
				HandleException(ex);
			}
		}

	}
}
